package com.company.project.wechat.officialAccounts.controller;

import com.company.project.common.aop.annotation.LogAnnotation;
import com.company.project.common.utils.DataResult;
import com.company.project.common.utils.ZxingUtil;
import com.company.project.wechat.officialAccounts.entity.UpdateFile;
import com.company.project.wechat.officialAccounts.service.WxOfficialAccountsService;
import com.company.project.wechat.commom.HttpUtils;
import com.google.zxing.WriterException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;


/**
  * @author ZYF
 * @date 2021/5/18 11:03
 */

@Api(tags = "微信公众号相关接口")
@RestController
@RequestMapping("/wxApi")
@Slf4j
public class WxControl {

    @Autowired
    WxOfficialAccountsService wxOfficialAccountsService;

    @Autowired
    HttpUtils httpUtils;

    @Autowired
    ZxingUtil zxingUtil;

    /**
     * 微信公众号获取AccountToken
     * @return AccountToken
     */
    @GetMapping("/getAccountToken")
    @ApiOperation(value = "微信公众号获取AccountToken")
    @LogAnnotation(title = "微信公众号获取AccountToken", action = "微信公众号获取AccountToken")
    public DataResult getAccountToken()  {
        return DataResult.success(wxOfficialAccountsService.getAccountToken());
    }

    /**
     * 微信公众号创建临时二维码
     * @return 二维码创建信息
     */
    @GetMapping("/getOrCodeUrl")
    @ApiOperation(value = "微信公众号创建临时二维码")
    @LogAnnotation(title = "微信公众号创建临时二维码", action = "微信公众号创建临时二维码")
    public DataResult getTicket(){
        HashMap<String,String> map = wxOfficialAccountsService.getTicket();
        return DataResult.success(map);
    }

    /**
     * 微信公众号获取临时二维码图片
     * @param response
     */
    @GetMapping("/getImage")
    @ApiOperation(value = "微信公众号获取临时二维码图片")
    @LogAnnotation(title = "微信公众号获取临时二维码图片", action = "微信公众号获取临时二维码图片")
    public void getImage(HttpServletResponse response){
        HashMap<String,String> map = wxOfficialAccountsService.getTicket();
        String ticket = map.get("ticket");
        String url = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket="+ticket;
        httpUtils.getImage(response,url);
    }

    @GetMapping("/getImageUrl")
    @ApiOperation(value = "微信公众号获取临时二维码图片Url")
    @LogAnnotation(title = "微信公众号获取临时二维码图片Url", action = "微信公众号获取临时二维码图片Url")
    public DataResult getImageUrl() throws IOException, WriterException {
        HashMap<String,String> map = wxOfficialAccountsService.getTicket();
        String url= map.get("orCodeUrl");
        return DataResult.success( zxingUtil.createQRImg(url,250, 250, "png"));
    }
    /**
     * 微信公众号上传素材
     * @param file
     * @return
     */
    @PostMapping("/upload")
    @ApiOperation(value = "微信公众号上传素材")
    @LogAnnotation(title = "微信公众号上传素材", action = "微信公众号上传素材")
    public DataResult upload(@RequestBody UpdateFile file) {
        return DataResult.success(wxOfficialAccountsService.upload(file));
    }
}
